/**
 * Copyright 2013 Mr.Hai Mobile Developer. All rights reserved.
 * HAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hector.meinewerk.controller;

import java.util.Vector;

import android.os.Bundle;

import com.hector.meinewerk.common.ActionEvent;
import com.hector.meinewerk.common.ActionEventConstant;
import com.hector.meinewerk.common.BaseActivity;
import com.hector.meinewerk.common.ErrorConstants;
import com.hector.meinewerk.common.ModelEvent;
import com.hector.meinewerk.constants.IntentConstants;
import com.hector.meinewerk.lib.network.http.HTTPRequest;
import com.hector.meinewerk.lib.network.http.NetworkUtil;

/**
 * 
 * abstract controller
 * 
 * @author: HaiTC3
 * @version: 1.1
 * @since: 1.0 | Mar 6, 2013
 */
public abstract class AbstractController {
	abstract public void handleViewEvent(ActionEvent e);

	abstract public void handleSwitchActivity(ActionEvent e);

	/**
	 * model return error
	 * 
	 * @author: HaiTC
	 * @param modelEvent
	 * @return: void
	 * @throws:
	 */
	public void handleErrorModelEvent(final ModelEvent modelEvent) {
		final ActionEvent e = modelEvent.getActionEvent();

		if (e.action == ActionEventConstant.LOGIN) {
			Bundle bundle = (Bundle) e.viewData;
			if (bundle != null) {
				Vector<String> userInfo = new Vector<String>();
				userInfo.add("loginName");
				userInfo.add(bundle
						.getString(IntentConstants.INTENT_LOGIN_PHONE));
				userInfo.add("phoneModel");
				userInfo.add(bundle
						.getString(IntentConstants.INTENT_LOGIN_PHONE_MODEL));
				userInfo.add("loginStatus");
				userInfo.add(bundle
						.getString(IntentConstants.INTENT_LOGIN_STATUS));
				StringBuffer strBuffer = new StringBuffer();
				strBuffer.append(NetworkUtil.getJSONString(
						"authenticate.login", userInfo));
			}

		}

		// end
		handleCommonError(modelEvent);
		BaseActivity ac = (BaseActivity) e.sender;
		HTTPRequest request = e.request;
		if (ac.isFinished) {
			return;
		}
		ac.runOnUiThread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				BaseActivity view = (BaseActivity) e.sender;
				view.handleErrorModelViewEvent(modelEvent);
			}

		});
		if (request != null) {
			ac.removeProcessingRequest(request, e.isBlockRequest);
			e.request = null;
		}
	}

	/**
	 * 
	 * Xu ly cac loi chung
	 * 
	 * @author: HaiTC
	 * @param modelEvent
	 * @return: void
	 * @throws:
	 */
	public void handleCommonError(ModelEvent modelEvent) {
		ActionEvent actionEvent = modelEvent.getActionEvent();
		switch (modelEvent.getModelCode()) {
		case ErrorConstants.ERROR_SESSION_RESET:
			actionEvent.controller = this;
			break;
		}
	}

	/**
	 * Xu ly du lieu tu model tra ve --> TH thanh cong
	 * 
	 * @author: HaiTC
	 * @param modelEvent
	 * @return: void
	 * @throws:
	 */
	public void handleModelEvent(final ModelEvent modelEvent) {
		final ActionEvent e = modelEvent.getActionEvent();
		BaseActivity ac = (BaseActivity) e.sender;
		HTTPRequest request = e.request;

		if (ac != null && !ac.isFinished
				&& (request == null || (request != null && request.isAlive()))) {
			ac.runOnUiThread(new Runnable() {
				public void run() {
					// TODO Auto-generated method stub
					BaseActivity base = (BaseActivity) e.sender;
					if (base != null) {
						base.handleModelViewEvent(modelEvent);
					}
				}
			});
			if (request != null) {
				ac.removeProcessingRequest(request, e.isBlockRequest);
				e.request = null;
			}
		}
	}
}
